package cn.htsyb.synergy.maker.mapping;

import java.util.HashMap;
import java.util.Map;

/**
 * MySQL数据库类型转换接口
 *
 * @author lakeside
 * @email hubin_yuan.410@hotmail.com
 * @date 2019-03-26
 */
public class MysqlTypeMapping {

    static Map<String, String> javaTypeMapping = new HashMap<String, String>() {{
        this.put("bit", "bit");
        this.put("tinyint unsigned", "Boolean");
        this.put("tinyint", "Boolean");
        /*this.put("tinyint", "Integer");*/
        this.put("bool", "Boolean");
        this.put("boolean", "Boolean");
        this.put("smallint", "Integer");
        this.put("mediumint", "Integer");
        this.put("int", "Integer");
        this.put("int unsigned", "Integer");
        this.put("integer", "Integer");
        this.put("bigint", "Long");
        this.put("float", "Float");
        this.put("double", "Double");
        this.put("decimal", "BigDecimal");

        /*Java8 规范*/
       /* this.put("date", "LocalDate");
        this.put("datetime", "LocalDateTime");
        this.put("timestamp", "Long");//"Instant"无法在springmvc的@RequestParam中转换 ，可使用@ModelAttribute的VO对象接收
        this.put("time", "LocalTime");
        this.put("year", "LocalDate");*/

        this.put("date", "Date");
        this.put("datetime", "Date");
        this.put("timestamp", "Long");
        this.put("time", "Date");
        this.put("year", "Date");
        this.put("json", "Object");

        this.put("char", "String");
        this.put("varchar", "String");
        this.put("binary", "byte[]");
        this.put("varbinary", "byte[]");
        this.put("tinyblob", "byte[]");
        this.put("blob", "byte[]");
        this.put("text", "String");
        this.put("mediumblob", "byte[]");
        this.put("mediumtext", "String");
        this.put("longblob", "byte[]");
        this.put("longtext", "String");
        this.put("enum", "String");
        this.put("set", "String");
        this.put("id", "Long");
    }};

    static Map<String, String> mybatisTypeMapping = new HashMap<String, String>() {
        {
            this.put("bit", "BIT");
            this.put("tinyint unsigned", "INTEGER");
            this.put("tinyint", "TINYINT");
            this.put("bool", "BOOLEAN");
            this.put("boolean", "BOOLEAN");
            this.put("smallint", "SMALLINT");
            this.put("mediumint", "INTEGER");
            this.put("int", "INTEGER");
            this.put("int unsigned", "INTEGER");
            this.put("integer", "INTEGER");
            this.put("bigint", "BIGINT");
            this.put("float", "FLOAT");
            this.put("double", "DOUBLE");
            this.put("decimal", "DECIMAL");
            this.put("date", "DATE");
            this.put("datetime", "TIMESTAMP");
            this.put("timestamp", "TIMESTAMP");
            this.put("time", "DATE");
            this.put("year", "DATE");

            this.put("json", "OBJECT");
            this.put("char", "CHAR");
            this.put("varchar", "VARCHAR");
            this.put("binary", "BLOB");
            this.put("varbinary", "BLOB");
            this.put("tinyblob", "BLOB");
            this.put("blob", "BLOB");
            this.put("text", "CLOB");
            this.put("mediumblob", "BLOB");
            this.put("mediumtext", "VARCHAR");
            this.put("longblob", "BLOB");
            this.put("longtext", "VARCHAR");
            this.put("enum", "VARCHAR");
            this.put("set", "VARCHAR");
            this.put("id", "Long");
        }
    };

    public static String convertToJavaType(String sqlType, Integer sqlSize) {
        sqlType = sqlType.toLowerCase();
        String givenType = sqlSize != null ? (sqlType + "(" + sqlSize + ")") : sqlType;
        if (javaTypeMapping.containsKey(givenType)) {
            return javaTypeMapping.get(sqlType);
        }
        return sqlType;
    }

    public static String convertToMyBatisType(String sqlType, Integer sqlSize) {
        sqlType = sqlType.toLowerCase();
        String givenType = sqlSize != null ? (sqlType + "(" + sqlSize + ")") : sqlType;
        if (mybatisTypeMapping.containsKey(givenType)) {
            return mybatisTypeMapping.get(sqlType);
        }
        return sqlType;
    }
}
